/**
 * 初始化详情对话框
 */
var PolicyPlanInfoDlg = {
    policyPlanInfoData : {},
    canSubmit : true,
    validateFields: {
        policyPlanCode: {
            validators: {
                notEmpty: {
                    message: '保障计划代码不能为空'
                }
            }
        },
        policyPlanName: {
            validators: {
                notEmpty: {
                    message: '保障计划名称不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
PolicyPlanInfoDlg.clearData = function() {
    this.policyPlanInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PolicyPlanInfoDlg.set = function(key, val) {
    this.policyPlanInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PolicyPlanInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
PolicyPlanInfoDlg.close = function() {
    parent.layer.close(window.parent.PolicyPlan.layerIndex);
}

/**
 * 收集数据
 */
PolicyPlanInfoDlg.collectData = function() {
    this
    .set('id')
    .set('policyId')
    .set('policyPlanCode')
    .set('policyPlanName')
    .set('comment');
}

/**
 * 验证数据
 */
PolicyPlanInfoDlg.validate = function () {
    $('#policyPlanInfoForm').data("bootstrapValidator").resetForm();
    $('#policyPlanInfoForm').bootstrapValidator('validate');
    return $("#policyPlanInfoForm").data('bootstrapValidator').isValid();
};
/**
 * 提交添加
 */
PolicyPlanInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    this.canSubmit = false;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/policyPlan/add", function(data){
        Feng.success("添加成功!");
        window.parent.PolicyPlan.table.refresh();
        PolicyPlanInfoDlg.canSubmit = true;
        PolicyPlanInfoDlg.close();
    },function(data){
        setTimeout(function () {
            PolicyPlanInfoDlg.canSubmit = true;
        },3000);
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.policyPlanInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
PolicyPlanInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/policyPlan/update", function(data){
        Feng.success("修改成功!");
        window.parent.PolicyPlan.table.refresh();
        PolicyPlanInfoDlg.canSubmit = true;
        PolicyPlanInfoDlg.close();
    },function(data){
        setTimeout(function () {
            PolicyPlanInfoDlg.canSubmit = true;
        },3000);
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.policyPlanInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("policyPlanInfoForm", PolicyPlanInfoDlg.validateFields);
    if($("#status").val() == 2){
        $("#policyPlanCode").attr('disabled','true');
        $("#policyPlanName").attr('disabled','true');
        $("#comment").attr('disabled','true');
    }
});
